<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    #code{ width: 100%; height: 100%; }
    .code{ width: 200px; height: 200px; margin: 60px; }
    #trace{ width: 320px; height: 120px; padding: 0; overflow-x: hidden; overflow-y: scroll; margin: 0; list-style: none; border-bottom: 1px solid #e0e0e0; }
    #trace li{ line-height: 30px; color: #666; }
    .container{ width: 320px; height: 320px; border: 1px solid #e0e0e0; margin: 200px auto 0 auto; }
    .container::-webkit-scrollbar{ display: none; }
</style>
<body>

<div class="container">
    <div class="code">
        <img id="code" />
    </div>
    <ul id="trace"></ul>
</div>

</body>

<script language="JavaScript">

    function Ajax(url, data, success, responseType, type) {
        //构造表单数据
        type = type ? type.toUpperCase() : 'POST';
        responseType = responseType ? responseType : 'text';
        //创建xhr对象
        var xhr = new XMLHttpRequest();
        //设置xhr请求的超时时间
        xhr.timeout = 10000;
        //设置响应返回的数据格式
        xhr.responseType = responseType;
        //创建一个 post 请求，采用异步
        if('GET' === type){
            var dataMap = [];
            for(var i in data){
                dataMap.push(i + '=' + data[i]);
            }
            url = url + '?' + dataMap.join('&');
        }
        xhr.open(type, url, true);
        if('POST' === type){
            if(data instanceof FormData){
                formData = data;
            }else{
                var formData = new FormData();
                for(var i in data){
                    formData.append(i, data[i]);
                }
            }
        }
        //注册相关事件回调处理函数
        xhr.onload = function(e) {
            if(this.status === 200 || this.status === 304){
                success && success(xhr.response);
            }
        };

        //发送数据
        xhr.send('POST' === type && formData);
    }

    function hash33(t) {
        for (var e = 0, i = 0, n = t.length; i < n; ++i)
            e += (e << 5) + t.charCodeAt(i);
        return 2147483647 & e
    }

    function ptuiCB(code, a, url, c, msg, nick){
        return {'code': code, url: url, msg: msg, nick: nick};
    }

    var trace = document.getElementById('trace');

    function ask(sig){
        Ajax('ask.php', {token: sig}, function(res){
            var arr = eval(res);
            var li = document.createElement('LI');
            var text = document.createTextNode(arr.msg + '-' + arr.nick);
            li.appendChild(text);
            trace.appendChild(li);
            trace.scrollTop += 30;
            if(arr.code == '0'){
                Ajax('login.php', {step: 2, url: arr.url}, function(step){

                });
            }else{
                setTimeout(function () {
                    ask(sig)
                }, 2000);
            }
        });
    }

    function getCookie(c_name)
    {
        if (document.cookie.length>0)
        {
            c_start=document.cookie.indexOf(c_name + "=")
            if (c_start!=-1)
            {
                c_start=c_start + c_name.length+1
                c_end=document.cookie.indexOf(";",c_start)
                if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
            }
        }
        return ""
    }

    document.addEventListener('DOMContentLoaded', function(){
        Ajax('login.php', {}, function(res){
            var code = document.getElementById('code');
            code.src = 'code.php';
            code.onload = function(){
                setTimeout(function () {
                    var sig = getCookie('qsig');
                    sig = hash33(decodeURIComponent(sig));
                    ask(sig);
                }, 1000);
            }

        }, 'text');
    });

</script>

</html>